define(["moment"], function (moment) {
    return ["$scope", "$rootScope","$filter", "$stateParams", "config", "MMWService", "$calculater",
    function ($scope,$rootScope, $filter, $stateParams, config, $MMWService, $calculater) {
        $scope.init = function(){
            $scope.origin_plot_no = '';
            $scope.origin_lot_list = {};
            $scope.terminal_lot_list = {};
            $scope.open_type = true;
        };

        // $scope.barcodeScan = function(event){
        //     if(event.keyCode == 13){
        //         $scope.search();
        //     }
        // };

        $scope.confirm = function(){
            if($scope.terminal_lot_list.lot_no == ''){
                $rootScope.showAlert($filter('translate')('SMT28.msg.termin_lot_no_empty'));
            }else{
                $MMWService.sendToServer({
                    uri: 'ServicesSMT.Module_SMT.assemble_save_process',
                    content: {
                        lot_no_src:$scope.origin_lot_list.lot_no,//來源生產批
                        lot_no_dest:$scope.terminal_lot_list.lot_no,//目的地生產批
                        serial_type:$scope.terminal_lot_list.type_no,//序號類別
                        serial_qty:$scope.terminal_lot_list.lot_no_qty,//序號數量
                        serial_start:$scope.terminal_lot_list.lot_no_from,//序號起始號
                        serial_end:$scope.terminal_lot_list.lot_no_to//序號結束號
                    },
                    success: function (data) {
                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                    }
                });
            }
        };
        
        $scope.load_origin_plot_no = function(){
            $rootScope.showQueryList({
                key:'lot_no',
                pageRecord: 50,
                // condition:[
                //     {name:$filter('translate')('SMT28.list_1.lot_no'), query_mode:'2', modeList:{name:$filter('translate')('common.report.condition.contain'), code:'2'},value:$scope.origin_plot_no}
                // ],
                header: [
                    { label: $filter('translate')('SMT28.list_1.lot_no'), code: 'lot_no' },
                    { label: $filter('translate')('SMT28.list_1.product_no'), code: 'product_no' },
                    { label: $filter('translate')('SMT28.list_1.product_ver'), code: 'product_ver' },
                    { label: $filter('translate')('SMT28.list_1.product_name'), code: 'product_name' },
                    { label: $filter('translate')('SMT28.list_1.qty'), code: 'qty' },
                    { label: $filter('translate')('SMT28.list_1.pcs_qty'), code: 'pcs_qty' },
                    { label: $filter('translate')('SMT28.list_1.pcs_start'), code: 'pcs_start' },
                    { label: $filter('translate')('SMT28.list_1.pcs_end'), code: 'pcs_end' },
                    { label: $filter('translate')('SMT28.list_1.panel_qty'), code: 'panel_qty' },
                    { label: $filter('translate')('SMT28.list_1.panel_start'), code: 'panel_start' },
                    { label: $filter('translate')('SMT28.list_1.panel_end'), code: 'panel_end' },
                ],
                query: function (conditions, feedback) {
                    console.log('conditions',conditions);
                    var content = {
                        query_condition_info: []
                    };
                    if(conditions.length > 0 && conditions[0].value && conditions[0].value != '')
                       content.query_condition_info.push({column_name: 'a.LOTNO', query_mode: '0', value: conditions[0].value, merge_condition_model: 'AND' });
                    $MMWService.sendToServer({
                        uri: 'ServicesSMT.Module_SMT.assemble_src_lot_info_get',
                        content,
                        success: function (data) {
                            feedback(data.lot_list);
                        }
                    });
                },
                selectCode: $scope.origin_plot_no,
                confirm: function (selectItem, dialog) {
                    $scope.origin_lot_list.lot_no = selectItem.lot_no;
                    $scope.origin_lot_list.product_no = selectItem.product_no;
                    $scope.origin_lot_list.product_ver = selectItem.product_ver;
                    $scope.origin_lot_list.product_name = selectItem.product_name;
                    $scope.origin_lot_list.qty = selectItem.qty;
                    $scope.origin_lot_list.pcs_qty = selectItem.pcs_qty;
                    $scope.origin_lot_list.pcs_start = selectItem.pcs_start;
                    $scope.origin_lot_list.pcs_end = selectItem.pcs_end;
                    $scope.origin_lot_list.panel_qty = selectItem.panel_qty;
                    $scope.origin_lot_list.panel_start = selectItem.panel_start;
                    $scope.origin_lot_list.panel_end = selectItem.panel_end;
                    dialog.hide();
                    $scope.open_type = false;
                }
            });
        }

        $scope.load_terminal_plot_no = function(){
            $rootScope.showQueryList({
                key:'lot_no',
                pageRecord: 50,
                header: [
                    { label: $filter('translate')('SMT28.list_1.lot_no'), code: 'lot_no' },
                    { label: $filter('translate')('SMT28.list_1.product_no'), code: 'product_no' },
                    { label: $filter('translate')('SMT28.list_1.product_ver'), code: 'product_ver' },
                    { label: $filter('translate')('SMT28.list_1.product_name'), code: 'product_name' },
                    { label: $filter('translate')('SMT28.list_1.item_spec'), code: 'item_spec' },
                    { label: $filter('translate')('SMT28.list_1.qty'), code: 'qty' }
                ],
                // condition:[
                //     {name:$filter('translate')('SMT28.list_1.lot_no'), query_mode:'2', modeList:{name:$filter('translate')('common.report.condition.contain'), code:'2'}, value:$scope.terminal_lot_list.lot_no}
                // ],
                query: function (conditions, feedback) {
                    console.log('conditions',conditions);
                    var content = {
                        query_condition_info: []
                    };

                    if(conditions.length > 0 && conditions[0].value && conditions[0].value != '')
                       content.query_condition_info.push({column_name: 'BASELOTNO', query_mode: '0', value: conditions[0].value, merge_condition_model: 'AND' });
                    $MMWService.sendToServer({
                        uri: 'ServicesSMT.Module_SMT.assemble_dest_lot_info_get',
                        content,
                        success: function (data) {
                            feedback(data.lot_list);
                        }
                    });
                },
                selectCode: $scope.terminal_lot_list.lot_no,
                confirm: function (selectItem, dialog) {
                    
                    if((+selectItem.qty) == (+$scope.origin_lot_list.qty)){
                        $scope.terminal_lot_list.lot_no = selectItem.lot_no;
                        $scope.terminal_lot_list.qty = selectItem.qty;
                    }else{
                        $rootScope.showAlert($filter('translate')('SMT28.msg.qty_not_right'));
                    }
                    dialog.hide();
                }
            });
        }

        $scope.load_lot_type = function(){
            var type_list =[
                {code: '1' , label:$filter('translate')('SMT28.list_3.column_1')},
                {code: '2' , label:$filter('translate')('SMT28.list_3.column_2')}
            ];
            $rootScope.showSelect({
                label : "label",
                code : 'code',
                selectCode : $scope.terminal_lot_list.type_no || '',
                list:type_list,
                confirm : function(selectItem, radio_dialog){
                    radio_dialog.hide();
                    $scope.terminal_lot_list.type_no = selectItem.code;
                    $scope.terminal_lot_list.type_name = selectItem.label;

                    if($scope.terminal_lot_list.type_no == '1'){//序號
                        $scope.terminal_lot_list.lot_no_qty = $scope.origin_lot_list.pcs_qty
                        $scope.terminal_lot_list.lot_no_from = $scope.origin_lot_list.pcs_start
                        $scope.terminal_lot_list.lot_no_to = $scope.origin_lot_list.pcs_end
                    }else if($scope.terminal_lot_list.type_no == '2'){//版號
                        $scope.terminal_lot_list.lot_no_qty = $scope.origin_lot_list.panel_qty
                        $scope.terminal_lot_list.lot_no_from = $scope.origin_lot_list.panel_start
                        $scope.terminal_lot_list.lot_no_to = $scope.origin_lot_list.panel_end
                    }
                }
            });
        }
    }
];});
